/**
 * ECGA Stat 统计系统脚本
 *
 * @author    sunfengming <sunfengming@ec3s.com>
 * @copyright 2011 Xingchangxinda Inc.
 * @version   SVN: $Id$
 * @todo 	  为JS进行Object闭包书写
 */

/**
 * PHP URLEncode 方法模拟
 * 
 * @param   {string} url url
 * 
 * @returns {string} encode url
 */
function statURLEncode(url) {
    var output = '';
    var x = 0;
    clearString = url.toString();
    var regex = /(^[a-zA-Z0-9-_.]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            if (clearString.substr(x, 1) == ' ') {
                //原文在此用 clearString[x] == ' ' 做判断, 但ie不支持把字符串当作数组来访问, 
                //修改后两种浏览器都可兼容 
                output += '+';
            } else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + (hexVal.length < 2 ? '0': '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}

/**
 * 获取Cookies
 * 
 * @param   string objName cookieName
 * 
 * @returns string cookie value
 */
function statGetCookie(objName){
	var arrStr = document.cookie.split("; ");
    for(var i = 0;i < arrStr.length;i ++){
		var temp = arrStr[i].split("=");
		if(temp[0] == objName) {
			return unescape(temp[1]);
		}
    }
	return false;
}

/**
 * 添加Cookie
 * 
 * @param   {string} objName Cookie名称
 * @param   {string} objValue Cookie值
 * @param   {int} objHours Cookie时间
 * 
 * @returns {void} 
 */
function statAddCookie(objName, objValue, objHours) {
    var str = objName + "=" + escape(objValue);
    if (objHours > 0) { //为0时不设定过期时间，浏览器关闭时cookie自动消失
        var date = new Date();
        var ms = objHours * 3600 * 1000;
        date.setTime(date.getTime() + ms);
        str += "; expires=" + date.toGMTString();
    }
    document.cookie = str;
}

/**
 * 删除Cookie，其实是让指定Cookies过期
 * 
 * @param   {string} name Cookie名称
 * 
 * @returns {void} 
 */
function statDelCookie(name) { 
    var date = new Date();
    date.setTime(date.getTime() - 10000);
    document.cookie = name + "=a; expires=" + date.toGMTString();
}

/**
 * 判断是否拥有访问记录
 * 
 * @returns {string} 记录Hash
 */
function statIsOldVisitor() {
	var oldHash = statGetCookie('stat_hash');
	if (false === oldHash) {
		statAddCookie('stat_hash', statHash, 24 * 365);
		return 'null';
	} else {
		return oldHash
	}
}

var statReferer = statURLEncode(document.referrer);

(function(){
	var imgDom = document.createElement('img');
	imgDom.id = 'stat_img';
	document.body.appendChild(imgDom);
	imgDom.style.width = '0px';
})();

window.onbeforeunload = function(e){
	var statImg = document.getElementById('stat_img');
	statImg.src = statGetUrl +
			'&stime=' + statStartTime +
			'&referer=' + statReferer +
			'&ip=' + statIp +
			'&sid=' + statSessionId +
			'&pid=' + statPid +
			'&sku=' + statSku +
			'&ua=' + statUA +
			'&hash=' + statOldHash +
			'&uri=' + statUri +
			'&rand=' + Math.random();
}

